package org.xurui.sxl.protocol.provider;

import lombok.extern.slf4j.Slf4j;
import org.xurui.sxl.protocol.command.CmdCodec;
import org.xurui.sxl.protocol.command.impl.*;
import org.xurui.sxl.protocol.utils.ClassUtils;

import java.util.ArrayList;
import java.util.List;

@Slf4j
public class CmdCodecProvider {

    List<CmdCodec> cmdCodecs = null;

    private static CmdCodecProvider singObj = null;

    private CmdCodecProvider() {
        super();
        initCodec();
    }

    public static CmdCodecProvider getInstance() {
        if (null == singObj ) {
            synchronized(CmdCodecProvider.class){
                if(singObj == null){
                    singObj = new CmdCodecProvider();
                }
            }
        }
        return singObj;
    }

    /**
     *
     * @param cmd 命令CMD
     * @param isRelay 是否为回复
     * @return
     */
    public CmdCodec getCmdCodecByCmd(String cmd,boolean isRelay){
        for(int i=0;i<cmdCodecs.size();i++){
            if(cmdCodecs.get(i).supportsCmd(cmd)){
                if(isRelay&&cmdCodecs.get(i).supportsRelayCmd()){
                    return cmdCodecs.get(i);
                }else{
                    return cmdCodecs.get(i);
                }
            }
        }
        return null;
    }
    public CmdCodec getCmdCodecByCmd(String cmd){
        return getCmdCodecByCmd(cmd,false);
    }

    //反射加载解码
    private void initCodec(){
        cmdCodecs = new ArrayList<>();
        cmdCodecs.add(new CmdCodec_BLOW());
        cmdCodecs.add(new CmdCodec_CALLDURATION());
        cmdCodecs.add(new CmdCodec_CALLFILTER());
        cmdCodecs.add(new CmdCodec_CALLFUN());
        cmdCodecs.add(new CmdCodec_CALLNOTE());
        cmdCodecs.add(new CmdCodec_CHOOSEID());
        cmdCodecs.add(new CmdCodec_CLASS());
        cmdCodecs.add(new CmdCodec_CLASSSWITCH());
        cmdCodecs.add(new CmdCodec_D1());
        cmdCodecs.add(new CmdCodec_D2());
        cmdCodecs.add(new CmdCodec_GETGPS());
        cmdCodecs.add(new CmdCodec_GETICCID());
        cmdCodecs.add(new CmdCodec_GETIMEID());
        cmdCodecs.add(new CmdCodec_GETPBWLALL());
        cmdCodecs.add(new CmdCodec_GETSMS());
        cmdCodecs.add(new CmdCodec_GETSTEP());
        cmdCodecs.add(new CmdCodec_GETWLALL());
        cmdCodecs.add(new CmdCodec_GJ());
        cmdCodecs.add(new CmdCodec_GJBAN());
        cmdCodecs.add(new CmdCodec_HOMEMAC());
        cmdCodecs.add(new CmdCodec_HOMESTATUS());
        cmdCodecs.add(new CmdCodec_HOMEWORK());
        cmdCodecs.add(new CmdCodec_HWFUALT());
        cmdCodecs.add(new CmdCodec_KA());
        cmdCodecs.add(new CmdCodec_MNC());
        cmdCodecs.add(new CmdCodec_MODE());
        cmdCodecs.add(new CmdCodec_MONITOR());
        cmdCodecs.add(new CmdCodec_MONITORNOTE());
        cmdCodecs.add(new CmdCodec_PBWL());
        cmdCodecs.add(new CmdCodec_PBWLALL());
        cmdCodecs.add(new CmdCodec_PDOP());
        cmdCodecs.add(new CmdCodec_PHONELEVEL());
        cmdCodecs.add(new CmdCodec_PINLOCK());
        cmdCodecs.add(new CmdCodec_POSTIN());
        cmdCodecs.add(new CmdCodec_POSTOUT());
        cmdCodecs.add(new CmdCodec_POSTSELF());
        cmdCodecs.add(new CmdCodec_POSTSELFM());
        cmdCodecs.add(new CmdCodec_POWEROFF());
        cmdCodecs.add(new CmdCodec_POWERON());
        cmdCodecs.add(new CmdCodec_R1());
        cmdCodecs.add(new CmdCodec_REJECTNOTE());
        cmdCodecs.add(new CmdCodec_REMIND());
        cmdCodecs.add(new CmdCodec_RFSWITCH());
        cmdCodecs.add(new CmdCodec_RING());
        cmdCodecs.add(new CmdCodec_RINGLEVEL());
        cmdCodecs.add(new CmdCodec_S23());
        cmdCodecs.add(new CmdCodec_S25());
        cmdCodecs.add(new CmdCodec_S2DOMAIN());
        cmdCodecs.add(new CmdCodec_SCHOOLIO());
        cmdCodecs.add(new CmdCodec_SCHOOLMAC());
        cmdCodecs.add(new CmdCodec_SCHOOLSTATUS());
        cmdCodecs.add(new CmdCodec_SENDSMS());
        cmdCodecs.add(new CmdCodec_SETICCID());
        cmdCodecs.add(new CmdCodec_SLEEPSTATUS());
        cmdCodecs.add(new CmdCodec_SLEEPTIME());
        cmdCodecs.add(new CmdCodec_SMSINFO());
        cmdCodecs.add(new CmdCodec_SMSSTATUS());
        cmdCodecs.add(new CmdCodec_SOS());
        cmdCodecs.add(new CmdCodec_SOSFUN());
        cmdCodecs.add(new CmdCodec_SPEAKER());
        cmdCodecs.add(new CmdCodec_STATIC());
        cmdCodecs.add(new CmdCodec_STATICSWITCH());
        cmdCodecs.add(new CmdCodec_STEP());
        cmdCodecs.add(new CmdCodec_STUINFO());
        cmdCodecs.add(new CmdCodec_TEMPERTURE());
        cmdCodecs.add(new CmdCodec_TTSLEVEL());
        cmdCodecs.add(new CmdCodec_V2());
        cmdCodecs.add(new CmdCodec_V3());
        cmdCodecs.add(new CmdCodec_WEATHER());
        cmdCodecs.add(new CmdCodec_WHITELIST());
        cmdCodecs.add(new CmdCodec_Wifimac());
        cmdCodecs.add(new CmdCodec_WIFISCAN());
        cmdCodecs.add(new CmdCodec_WLALL());
//        List<Class> classes = ClassUtils.getAllClassByInterface(CmdCodec.class);
//        for (Class clas :classes) {
//            try {
//                cmdCodecs.add((CmdCodec) clas.newInstance());
//            } catch (Exception e) {
//                log.error("初始化上学啦CMD解码器失败",e);
//            }
//        }
    }



}
